<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName()
            + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>

<head>
    <title>车位预定</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- 引入css样式文件 -->
    <!-- Bootstrap Core CSS -->
    <link href="<%=basePath%>css/bootstrap.css" rel="stylesheet"/>
    <!-- Bootstrap-grid CSS -->
    <link href="<%=basePath%>css/bootstrap-grid.css" rel="stylesheet"/>
    <!-- Bootstrap-reboot CSS -->
    <link href="<%=basePath%>css/bootstrap-reboot.css" rel="stylesheet"/>
    <!-- MetisMenu CSS -->
    <link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet" />
    <!-- DataTables CSS -->
    <link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet" />
    <!-- Custom CSS -->
    <link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet" />
    <!-- Custom Fonts -->
    <link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet" type="text/css" />
    <link href="<%=basePath%>css/boot-crm.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>

<div>
    <nav class="navbar navbar-expand-lg navbar-dark " style="background-color:#81D8CF;;">
        <img src="imges/logo.jpg" class="rounded float-left" alt="Logo" height="45" width="45">
        <div class="container" style="">
            <a class="navbar-brand" href="#">停车场管理系统</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText"
                    aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarText">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item">
                        <a class="nav-link " href="${pageContext.request.contextPath }/userself">个人信息</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="${pageContext.request.contextPath }/bookself">车位预定</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="${pageContext.request.contextPath}/payments">全部订单</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="${pageContext.request.contextPath}/coupon">优惠券</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="${pageContext.request.contextPath }/rateself">收费标准</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="${pageContext.request.contextPath }/vehicleself">车辆信息</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="${pageContext.request.contextPath }/userequip">投诉反馈</a>
                    </li>
                </ul>
                <span class="navbar-text">
          导航栏
        </span>
            </div>
        </div>
    </nav>
</div>
<%

    if (session.getAttribute("USER_SESSION")!=null) {
%>
<!--登录成功界面-->



<div class="container">
    <div class="p-2 bg-light border">
        <div class="row">

            <div class="panel panel-default">
                <div class="panel-body">
                    <form class="form-inline" method="get" action="${pageContext.request.contextPath }/bookself">
                        <div class="form-group">
                            <label for="beginTime">开始时间</label>
                            <input type="datetime-local" class="form-control" id="beginTime"
                                   value="${beginTime}" name="beginTime"/>
                        </div>
                        <div class="form-group">
                            <label for="endTime">结束时间</label>
                            <input type="datetime-local" class="form-control" id="endTime"
                                   value="${endTime}" name="endTime"/>
                        </div>
                        <div class="form-group">
                            <label for="status">状态</label>
                            <select name="status" id="status" class="form-control" value="${status}">
                                <option value=""></option>
                                <option value="已预定">已预定</option>
                                <option value="已取消">已取消</option>
                            </select>
                        </div>

                        <button type="submit" class="btn btn-primary">查询</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <a href="#" class="btn btn-primary" data-toggle="modal"
       data-target="#newBookDialog" onclick="clearBook()">新建</a>
    <div class="p-2 bg-light border">
        <table class="table table-striped">
            <thead>
            <tr class="text-center">
                <th>编号</th>
                <th>用户名</th>
                <th>开始时间</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${page.rows}" var="row">
                <tr class="text-center">
                    <td>${row.bookId}</td>
                    <td>${row.user.username}</td>
                    <td>${row.startTime}</td>
                    <td>${row.status}</td>
                    <td>
                        <a href="#" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#bookEditDialog"
                           onclick="editBook(${row.bookId})">修改</a>
                        <a href="#" class="btn btn-danger btn-xs" onclick="deleteBook(${row.bookId})">删除</a>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
        <div class="col-md-12 text-right">
            <ujn:page url="${pageContext.request.contextPath }/book"/>
        </div>
    </div>
</div>

<!-- 创建模态框 -->
<div class="modal fade" id="newBookDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel1">新增预定</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="new_user_form">
                    <div class="form-group">
                        <label for="new_userId" class="col-sm-3 control-label">
                            用户名
                        </label>
                        <div class="col-sm-10">

                            <select name="userId" id="new_userId" class="form-control"  >
                                <c:forEach items="${userList}" var="user">
                                    <option value=${user.userId}>${user.username}</option>
                                </c:forEach>
                            </select>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="new_startTime" class="col-sm-4 control-label">开始时间</label>
                        <div class="col-sm-10">
                            <input type="datetime-local" class="form-control" id="new_startTime" placeholder="开始时间" name="startTime" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="new_status" class="col-sm-2 control-label">状态</label>
                        <div class="col-sm-10">
                            <select name="status" id="new_status" class="form-control" value="${status}" >
                                <option value="已预定">已预定</option>
                                <option value="已取消">已取消</option>
                            </select>
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

                <button type="button" class="btn btn-primary" onclick="createBook()">新增用户</button>

            </div>
        </div>
    </div>
</div>
<!-- 修改模态框 -->
<div class="modal fade" id="bookEditDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel2">修改预定信息</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="edit_user_form">
                    <input type="hidden" id="edit_bookId" name="bookId"/>
                    <div class="form-group">
                        <label for="edit_userId" class="col-sm-4 control-label">用户名称</label>
                        <div class="col-sm-10">

                            <select name="userId" id="edit_userId" class="form-control" name="userId" >
                                <c:forEach items="${userList}" var="user">
                                    <option value=${user.userId}>${user.username}</option>
                                </c:forEach>
                            </select>
                        </div>

                    </div>

                    <div class="form-group">
                        <label for="edit_startTime" class="col-sm-4 control-label">开始时间</label>
                        <div class="col-sm-10">
                            <input type="datetime-local" class="form-control" id="edit_startTime" placeholder="开始时间" name="startTime" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_status" class="col-sm-2 control-label">状态</label>
                        <div class="col-sm-10">
                            <select name="status" id="edit_status" class="form-control"  >
                                <option value="已预定">已预定</option>
                                <option value="已取消">已取消</option>
                            </select>
                        </div>

                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="updateBook()">保存修改</button>
            </div>
        </div>
    </div>
</div>
<%
} else{
%>
<!--登陆失败界面-->
<div class="jumbotron jumbotron-fluid">
    <div class="container">
        <h1 class="display-4">欢迎您,登录后即可查看</h1>
        <p class="lead">
            <a href="${pageContext.request.contextPath}/login">Welcome, log in to view</a>
        </p>
    </div>
</div>
<%  }
%>
<!-- 引入js文件 -->
<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<%=basePath%>js/bootstrap.js"></script>
<!-- Bootstrap-bundle JavaScript -->
<script src="<%=basePath%>js/bootstrap.bundle.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>
<!-- 编写js代码 -->
<script type="text/javascript">
    //清空新建窗口中的数据
    function clearBook() {
        $("#new_userId").val("");
        $("#new_startTime").val("")
        $("#new_status").val("")

    }


    // 创建
    function createBook() {

        var userId = $("#new_userId").val();
        var startTimeString = $("#new_startTime").val();

        var dateObject = new Date(startTimeString);
        var startTimeISO = dateObject.toISOString();
        console.log(startTimeString)
        console.log(dateObject)
        console.log(startTimeISO)
        var status = $("#new_status").val();
        $.ajax({
            url: '<%=basePath%>book',
            type: 'POST',
            data: JSON.stringify({
                userId: userId,
                startTime: startTimeISO,
                status: status
            }),
            contentType: "application/json;charset=UTF-8",
            success: function (data) {
                if (data == "OK") {
                    alert("用户信息更新成功！");
                    window.location.reload();
                } else {
                    alert("用户信息更新失败！");
                    window.location.reload();
                }
            }
        });
    }


    function formatDate(date) {
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份从0开始，所以需要加1
        var day = ('0' + date.getDate()).slice(-2);
        var hours = ('0' + date.getHours()).slice(-2);
        var minutes = ('0' + date.getMinutes()).slice(-2);

        return year + '-' + month + '-' + day + "T" + hours + ':' + minutes;
    }
    // 通过id获取修改的用户信息
    function editBook(id) {
        $.ajax({
            type: "get",
            url: "<%=basePath%>book/" + id,
            success: function (data) {
                $("#edit_bookId").val(data.bookId);
                $("#edit_userId").val(data.userId);
                $("#edit_startTime").val(formatDate(new Date(data.startTime)));
                $("#edit_status").val(data.status)

                console.log(new Date(data.startTime))
                console.log(new Date(data.startTime).getFullYear())
                console.log(formatDate(new Date(data.startTime)))
            }
        });

    }

    // 执行修改操作
    function updateBook() {
        var bookId = $("#edit_bookId").val();
        var userId = $("#edit_userId").val();
        var startTime = $("#edit_startTime").val();
        var dateObject = new Date(startTime);
        var startTimeISO = dateObject.toISOString();
        var status = $("#edit_status").val();
        $.ajax({
            url: '<%=basePath%>book',
            type: 'PUT',
            data: JSON.stringify({
                bookId: bookId,
                userId: userId,
                startTime: startTimeISO,
                status: status
            }),
            contentType: "application/json;charset=UTF-8",
            success: function (data) {
                if (data == "OK") {
                    alert("用户信息更新成功！");
                    window.location.reload();
                } else {
                    alert("用户信息更新失败！");
                    window.location.reload();
                }
            }
        });
    }

    // 删除
    function deleteBook(id) {
        if (confirm('确实要删除该用户吗?')) {
            $.ajax({
                url: '<%=basePath%>book/' + id,
                type: 'DELETE',
                success: function (data) {
                    if (data == "OK") {
                        alert("用户信息删除成功！");
                        window.location.reload();
                    } else {
                        alert("用户信息删除失败！");
                        window.location.reload();
                    }
                }
            });
        }
    }
</script>
</body>

</html>
